package com.qq.controller;
import com.qq.dto.ActionResult;
import com.qq.pojo.SysUser;

import com.qq.service.SysUserService;

import com.qq.utils.CheckMunber;
import com.qq.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    private  String a;
@Autowired
private SysUserService userService;

    /**
     * 注册
     * @param
     * @param
     * @return
     */
    @RequestMapping("/registController")
@ResponseBody
    private Map registController(SysUser user){
        HashMap<Object, Object> map = new HashMap<>();
        SysUser byTel = userService.findByTel(user.getTel());
        if (byTel!=null){
            map.put("info","手机号已经存在,注册失败");
        }else {
        int i = userService.insertUser(user);

        if (i > 0){
            map.put("code",1);

        }else {
            map.put("info","注册失败");
        }
        return map;
    }
        return map;
    }
/**
 *登录
 */
@ResponseBody
@RequestMapping("/findUserController")
public Map findUserController(String tel, String password, HttpSession session){

    SysUser user = userService.findUser(tel, password);
/*    String invitation = user.getInvitation();
    System.out.println("呈贡"+invitation);*/
    SysUser byinvitation = userService.findByinvitation(tel);
    String invitation = byinvitation.getInvitation();
    System.out.println(user);
    int id = user.getId();
    session.setAttribute("tel",tel);
    Map<Object,Object> map=new HashMap<>();

/*    if (user!=null){
        map.put("code",1);
    }else {
        map.put("info","登录失败");
    }*/
    //管理员用户
    if (user!=null&&invitation!=null){
        System.out.println("123"+invitation);
        map.put("codes",2);
    }
    //普通用户
    if (user!=null&&invitation==null){
        map.put("code",1);
        map.put("uid",id);
    }else {
        map.put("info","密码错误");
    }
   return map;
}

    /**
     * 显示登录者
     * @param session
     * @return
     */
    @RequestMapping("/getTel")

    public @ResponseBody String getTelController(HttpSession session){
    String tel = (String) session.getAttribute("tel");
    return tel;

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findUserById",method = RequestMethod.GET)
    public ActionResult findUserById(@RequestParam("id") int id){
        System.out.println("id="+id);
        SysUser user = userService.findById(id);
        //System.out.println(user);
        ActionResult result = new ActionResult(200, null, user);
        return result;
    }
    /**
     * 修改动态sql
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateAll",method = RequestMethod.PUT)
    public ActionResult updateAll(@RequestBody String tel, @RequestParam("id") int id){
        System.out.println(id+"----"+tel);
        String s = tel.split("=")[1];
        System.out.println(s);
        userService.bigUpdate(s,id);
        return new ActionResult(200,"delete seccess",null);
    }
    /**
     * 邮箱验证
     */
    @ResponseBody
    @RequestMapping(value = "/back",method = RequestMethod.POST)
    public Map loginUser(String email){
        String c = CheckMunber.check();
        a = c;
        System.out.println(c);
        System.out.println(email);
        MailUtils.sendMail(email,c,"验证码");
        Map<String,Object> map = new HashMap();
        map.put("code","发送成功，请接受验证码");
        return map;
    }

    /**
     * 根据邮箱查询
     * @param email
     * @param uuid
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findUserEmailone",method = RequestMethod.POST)
    public ActionResult findUserEmail1(String email, String uuid){
        System.out.println("---------");
        System.out.println(email);
        if (uuid.equals(a)){
            SysUser userEmail = userService.findUserEmail(email);
            System.out.println(userEmail);
            ActionResult result = new ActionResult(200, null, userEmail.getId());
            return result;
        }
        return null;
    }
    /**
     * 根据根据邮箱查询
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findUserEmailtwo",method = RequestMethod.GET)
    public ActionResult findUserEmailtwo(@RequestParam int id){
        SysUser user = userService.findById(id);
        System.out.println(user);

        return new ActionResult(200,null,user);
    }
    /**
     * 根据id修改密码
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateUserPassword",method = RequestMethod.PUT)
    public ActionResult updateUserPassword(int id,String password){
        userService.updatePasswordById(password,id);

        return new ActionResult(200,"update success",null);
    }
}
